package com.biz.primus.ms.commodity.api;

import com.biz.primus.base.vo.IdNameVo;
import com.biz.primus.base.vo.PageResult;
import com.biz.primus.commodity.vo.CategoryAndBrandVo;
import com.biz.primus.commodity.vo.CategoryQueryVo;
import com.biz.primus.commodity.vo.SyncCategoryVo;
import com.biz.primus.commodity.vo.backend.*;
import com.biz.primus.common.vo.SpringPageVO;
import com.biz.primus.ms.base.api.BaseApiController;
import com.biz.primus.ms.commodity.dao.redis.CategoryRedisDao;
import com.biz.primus.ms.commodity.dao.repository.category.CategoryRepository;
import com.biz.primus.ms.commodity.service.backend.CategoryService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yanweijin
 * @date 2017/9/12
 */
@RestController
@RequestMapping("soa/categoryService")
public class CategoryApiController extends BaseApiController {

    private final CategoryRepository categoryRepository;

    private final CategoryRedisDao categoryRedisDao;

    private final CategoryService categoryService;

    @Autowired
    public CategoryApiController(CategoryRepository categoryRepository, CategoryRedisDao categoryRedisDao, CategoryService categoryService) {
        this.categoryRepository = categoryRepository;
        this.categoryRedisDao = categoryRedisDao;
        this.categoryService = categoryService;
    }

    @ApiOperation("创建分类")
    @PostMapping("createCategory")
    CreateCategoryVo createCategory(@RequestBody CreateCategoryVo createCategoryVo){
        return categoryService.createCategory(createCategoryVo);
    }

    @ApiOperation("编辑分类")
    @PostMapping("updateCategory")
    void updateCategory(@RequestBody UpdateCategoryVo updateCategoryVo) {
        categoryService.updateCategory(updateCategoryVo);
    }

    @ApiOperation("删除分类")
    @DeleteMapping("deleteCategory")
    boolean deleteCategory(@RequestParam("id") Long id) {
        return categoryService.deleteCategory(id);
    }

    @ApiOperation("获取当前层级分类的最大 Idx")
    @GetMapping("getCategoryMaxIdx")
    Integer getCategoryMaxIdx(@RequestParam("parentCategoryId") Long parentCategoryId){
        return categoryService.getCategoryMaxIdx(parentCategoryId);
    }

    @ApiOperation("返回给前端展示 TreeView")
    @GetMapping("getCategoryTreeViewVos")
    List<CategoryTreeViewVo> getCategoryTreeViewVos(){
        return categoryService.getCategoryTreeViewVos();
    }

    @ApiOperation("列出所有的分类")
    @PostMapping("findAll")
    SpringPageVO<CategoryListItemVo> findAll(@RequestBody CategoryQueryVo reqVo){
        return categoryService.findAll(reqVo);
    }

    @ApiOperation("查找分类")
    @PostMapping("backend/findAllCategory")
    BootstrapTablePageResult<CategoryListVo> findAllCategory(@RequestBody SearchPageVo pageVo){
        return categoryService.findAllCategory(pageVo);
    }

    @ApiOperation("将所有的分类信息缓存到内存当中去")
    @PostMapping("loadCategory2Cache")
    void loadCategory2Cache(){
        categoryService.loadCategory2Cache();
    }

    @ApiOperation("通过categoryId获取category 基本信息")
    @GetMapping("getCategoryItem")
    CategoryItemVo getCategoryItem(@RequestParam("id") Long categoryId){
        return categoryService.getCategoryItem(categoryId);
    }

    @ApiOperation("获取 分类 编辑信息")
    @GetMapping("getCategoryById")
    UpdateCategoryVo getCategoryById(@RequestParam("id") Long categoryId){
        return categoryService.getCategoryById(categoryId);
    }

    @ApiOperation("保存排序")
    @PostMapping("saveOrUpdateSort")
    void saveOrUpdateSort(@RequestBody CategorySortVo vo){
        categoryService.saveOrUpdateSort(vo);
    }

    @ApiOperation("根据分类id集合查询分类")
    @PostMapping("findCategoryByIds")
    List<IdNameVo> findCategoryByIds(@RequestBody List<Long> ids){
        return categoryService.findCategoryByIds(ids);
    }

    @ApiOperation("根据分类名称分页查询分类")
    @PostMapping("findCategoryByName")
    PageResult<IdNameVo> findCategoryByName(@RequestBody SearchPageVo searchPageVo) {
        return categoryService.findCategoryByName(searchPageVo);
    }

    @ApiOperation("获得下拉分类结果")
    @GetMapping("getSelectCategory")
    List<IdNameVo> getSelectCategory(){
        return categoryService.getSelectCategory();
    }

    @ApiOperation("获取到最顶层的分类")
    @GetMapping("getTopCategories")
    List<IdNameVo> getTopCategories(){
        return categoryService.getTopCategories();
    }

    @ApiOperation("获取所有分类")
    @GetMapping("frontend/findAllCategory")
    List<CategoryAndBrandVo> findAllCategory(){
        return categoryService.findAllCategory();
    }

    @ApiOperation("获取App需要的分类")
    @GetMapping("frontend/findAllCategoryForApp")
    List<IdNameVo> findAllCategoryForApp(){
        return categoryService.findAllCategoryForApp();
    }


    @ApiOperation("erp同步分类")
    @PostMapping("syncCategories")
    void syncCategories(@RequestBody List<SyncCategoryVo> syncCategoryVos){
        categoryService.syncCategories(syncCategoryVos);
    }
}
